<template>
    <div class="app-container">
      <el-row :gutter="20">
<!--        顶部数据加载栏-->
        <el-col :span="24" :xs="24">
          <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
            <el-form-item label="更新时间">
              <el-date-picker
                v-model="dateRange"
                size="small"
                style="width: 240px"
                value-format="yyyy-MM-dd"
                type="daterange"
                range-separator="-"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              ></el-date-picker>
            </el-form-item>
            <el-form-item label="测项名称" prop="staName">
              <el-input
                v-model="queryParams.testName"
                placeholder="测项名称"
                clearable
                size="small"
                style="width: 180px"
              />
            </el-form-item>
            <el-form-item label="缺数原因" prop="remark">
              <el-input  v-model="queryParams.remark"
                         placeholder="变化及缺数原因"
                         clearable
                         size="small"
                         style="width: 180px"/>
            </el-form-item>
            <el-form-item>
              <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
            </el-form-item>
          </el-form>
          <el-form>
            <el-form-item>
              <el-button
                type="primary"
                plain
                icon="el-icon-plus"
                size="mini"
                @click="handleAdd"
                v-hasPermi="['library:base:add']"
              >新增</el-button>
              <el-button
                type="primary"
                plain
                size="mini"
                @click="loadYesData"
              >加载昨日数据</el-button>
            </el-form-item>
          </el-form>
          <el-row :gutter="10" class="mb8">
            <el-table size="mini" v-loading="loading" :data="stationList">
              <el-table-column label="编号" align="center" prop="id" v-if="columns[0].visible" />
              <el-table-column label="台站名称" align="center" prop="staName" v-if="columns[1].visible" />
              <el-table-column label="测项名称" align="center" prop="testName" v-if="columns[2].visible" />
              <el-table-column label="变化及缺数原因" align="center" key="remark" prop="remark" v-if="columns[3].visible"/>
              <el-table-column label="最新资料日期" align="center" key="createTime" prop="createTime" v-if="columns[4].visible"/>
              <el-table-column label="操作人" align="center" key="operator" prop="operator" v-if="columns[5].visible"/>
              <el-table-column
                label="操作"
                align="center"
                width="160"
                class-name="small-padding fixed-width"
              >
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['library:base:edit']"
                  >修改</el-button>
                  <el-button
                    v-if="scope.row.scid !== 1"
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['library:base:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>

            <pagination
              v-show="total>0"
              :total="total"
              :page.sync="queryParams.pageNum"
              :limit.sync="queryParams.pageSize"
              @pagination="getList"
            />
          </el-row>
        </el-col>
      </el-row>

      <!-- 添加或修改参数配置对话框 -->
      <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="终端ID" prop="id">
                <el-input v-model="form.id" readonly="readonly" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="台站名称" prop="staName">
                <el-input v-model="form.staName" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="12">
              <el-form-item label="测项名称" prop="testName">
                <el-input v-model="form.testName" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="操作人" prop="operator">
                <el-input v-model="form.operator" />
              </el-form-item>
            </el-col>
          </el-row>
          <el-row>
            <el-col :span="24">
              <el-form-item label="变化及缺数原因" prop="remark">
                <el-input type="textarea" v-model="form.remark" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
</template>

<script>
  import { listStation,updateStation,getOneStation,addStation,delStation } from "@/api/qzsta/station";
  import {getTime} from "../../../utils";
  import {parseTime} from "../../../utils/ruoyi";

  export default {
    name: "index",
    data(){
      return{
        // 遮罩层
        loading: true,
        // 台站数据
        stationList: null,
        // 日期范围
        dateRange: [],
        // 是否显示弹出层
        open: false,
        // 表单参数
        form: {},
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          orderByColumn:"id",
          isAsc:"desc",
        },
        // 状态数据字典
        statusOptions: [],
        // 列信息
        columns: [
          { key: 0, label: `编号`, visible: false },
          { key: 1, label: `台站名称`, visible: true },
          { key: 2, label: `测项名称`, visible: true },
          { key: 3, label: `变化及缺数原因`, visible: true },
          { key: 4, label: `最新资料日期`, visible: true },
          { key: 5, label: `操作人`, visible: true },
        ],
        // 表单校验
        rules: {
          staName: [
            { required: true, message: "台站名称不能为空", trigger: "blur" }
          ],
          testName: [
            { required: true, message: "测项名称不能为空", trigger: "blur" }
          ],
        },
        // 总条数
        total: 0,
        // 弹出层标题
        title: "",
      }
    },
    created() {
      this.getList();
    },
    methods:{
      getList(){
        // console.log(this.dateRange);
        this.loading = true;
        listStation(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
            this.stationList = response.rows;
            this.total = response.total;
            this.loading = false;
          }
        );
      },
      loadYesData(){
        // console.log(parseTime(new Date().getTime()-24*60*60*1000).substr(0,10));
        this.dateRange=[parseTime(new Date().getTime()-24*60*60*1000).substr(0,10),parseTime(new Date().getTime()-24*60*60*1000).substr(0,10),];
        this.getList();
        },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset();
        const staId = row.id;
        getOneStation(staId).then(response => {
          this.form = response.data;
          this.open = true;
          this.title = "修改日志记录";
        });
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1;
        this.getList();
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset();
        this.open = true;
        this.title = "新增日志记录";
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const staIds = row.id || this.ids;
        this.$confirm('是否确认删除用户编号为"' + staIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delStation(staIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        }).catch(() => {});
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              updateStation(this.form).then(response => {
                this.msgSuccess("修改成功");
                this.open = false;
                this.getList();
              });
            } else {
              addStation(this.form).then(response => {
                this.msgSuccess("新增成功");
                this.open = false;
                this.getList();
              });
            }
          }
        });
      },
      // 取消按钮
      cancel() {
        this.open = false;
        this.reset();
      },
      // 表单重置
      reset() {
        this.form = {
          id: undefined,
          staName: undefined,
          testName: undefined,
          remark: undefined,
          createTime: undefined,
          operator:undefined
        };
        this.resetForm("form");
      }
    }
  }
</script>

<style scoped>

</style>
